You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[customId].js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { Button, Grid, Tab, Tabs, Typography } from '@mui/material';
  2. import { Box, Container } from '@mui/system';
  3. import { dehydrate, QueryClient } from '@tanstack/react-query';
  4. import Image from 'next/image';
  5. import { useRouter } from 'next/router';
  6. import React, { useState } from 'react';
  7. import Loader from '../../components/loader/Loader';
  8. import ProductCard from '../../components/product-card/ProductCard';
  9. import TabPanel from '../../components/tab-panel/TabPanel';
  10. import { useFetchSingleProduct } from '../../hooks/useFetchProductData';
  11. import { getProductData } from '../../requests/products/producDataRequest';
  12. import { useStore, useStoreUpdate } from '../../store/cart-context';
  13. const SingleProduct = () => {
  14. const { addCartValue } = useStoreUpdate();
  15. const { cartStorage } = useStore();
  16. const router = useRouter();
  17. const { customId } = router.query;
  18. const { data, isLoading } = useFetchSingleProduct(customId);
  19. // const productCategory = data?.product.category;
  20. const [value, setValue] = useState(0);
  21. const addProductToCart = (quantity) => addCartValue(data.product, quantity);
  22. const inCart = cartStorage?.some(
  23. (item) => item.product.customID === data?.product.customID
  24. )
  25. ? true
  26. : false;
  27. const handleChange = (event, newValue) => {
  28. setValue(newValue);
  29. };
  30. function a11yProps(index) {
  31. return {
  32. id: `simple-tab-${index}`,
  33. 'aria-controls': `simple-tabpanel-${index}`,
  34. };
  35. }
  36. if (isLoading) {
  37. return <Loader loading={isLoading} />;
  38. }
  39. return (
  40. <Box
  41. sx={{
  42. display: 'flex',
  43. flexDirection: 'column',
  44. }}
  45. >
  46. <Container>
  47. <Typography
  48. fontFamily={'body1.fontFamily'}
  49. fontSize="32px"
  50. sx={{ mt: 25, height: '100%', color: 'primary.main' }}
  51. >
  52. {data.product.name}
  53. </Typography>
  54. <Grid container spacing={2}>
  55. <Grid sx={{ display: 'flex' }} item md={6} sm={12}>
  56. <Image
  57. src={data.product.image}
  58. alt="product"
  59. width={900}
  60. height={700}
  61. />
  62. </Grid>
  63. <Grid item xs={12} md={6}>
  64. <Tabs
  65. sx={{
  66. '& button:focus': {
  67. borderTop: '1px solid black',
  68. borderLeft: '1px solid black',
  69. borderRight: '1px solid black',
  70. borderRadius: '5px 5px 0 0',
  71. borderBottom: 'none',
  72. },
  73. }}
  74. value={value}
  75. onChange={handleChange}
  76. aria-label="basic tabs example"
  77. >
  78. <Tab
  79. sx={{
  80. width: '50%',
  81. }}
  82. label="Purchase"
  83. {...a11yProps(0)}
  84. />
  85. <Tab sx={{ width: '50%' }} label="Category" {...a11yProps(1)} />
  86. </Tabs>
  87. <TabPanel value={value} index={0}>
  88. <Box flexGrow={2} sx={{ pb: { xs: '70px' } }}>
  89. <Typography>{data.product.description}</Typography>
  90. </Box>
  91. <Box
  92. sx={{
  93. display: { xs: 'flex' },
  94. flexDirection: { xs: 'column' },
  95. justifyContent: { xs: 'center' },
  96. alignItems: { xs: 'center', md: 'flex-end' },
  97. }}
  98. >
  99. <Typography mb={2}>${data.product.price}</Typography>
  100. <Button
  101. disabled={inCart}
  102. onClick={() => addProductToCart(1)}
  103. sx={{
  104. backgroundColor: '#CBA213',
  105. height: 50,
  106. width: { xs: '300px', md: '150px' },
  107. color: 'white',
  108. }}
  109. >
  110. {inCart ? 'In Cart' : 'Add to cart'}
  111. </Button>
  112. </Box>
  113. </TabPanel>
  114. <TabPanel value={value} index={1}>
  115. <Box sx={{ mb: { xs: '60px' } }}>{data.product.category}</Box>
  116. </TabPanel>
  117. </Grid>
  118. </Grid>
  119. <Typography
  120. sx={{
  121. mt: { xs: '60px', md: '100px', lg: '150px' },
  122. mb: 5,
  123. color: 'primary.main',
  124. fontSize: '32px',
  125. }}
  126. >
  127. Similar Products You May Like
  128. </Typography>
  129. <Grid container spacing={2}>
  130. {data.similarProducts.map((product) => (
  131. <Grid
  132. key={product._id}
  133. item
  134. md={4}
  135. sm={6}
  136. xs={12}
  137. sx={{ mb: '100px' }}
  138. >
  139. <ProductCard product={product} />
  140. </Grid>
  141. ))}
  142. </Grid>
  143. </Container>
  144. </Box>
  145. );
  146. };
  147. export const getServerSideProps = async (context) => {
  148. const { params } = context;
  149. const { customId } = params;
  150. const queryClient = new QueryClient();
  151. await queryClient.prefetchQuery(
  152. ['product', customId],
  153. async () => await getProductData(customId)
  154. );
  155. return {
  156. props: {
  157. dehydratatedState: dehydrate(queryClient),
  158. },
  159. };
  160. };
  161. export default SingleProduct;